/************************************************************************
**
** Authors:   Ulf Hermann <ulfonk_mennhar@gmx.de> (Alve), 
**            Marek Krejza <krejza@gmail.com> (Caligor)
**
** This file is part of the MMapper2 project. 
** Maintained by Marek Krejza <krejza@gmail.com>
**
** Copyright: See COPYING file that comes with this distribution
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
** and appearing in the file COPYING included in the packaging of
** this file.  
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
**
*************************************************************************/

#include <iostream>
#include "component.h"

using namespace std;




map<QString, ComponentCreator *> & ComponentCreator::creators() {
      static map<QString, ComponentCreator *> creators;
      return creators;
}

Component::Component(bool threaded) {
	if (threaded) {
	  thread = new ComponentThreader(this);
	}
	else {
	  thread = 0;
	}
}

void Component::start() {
  if (thread) {
    moveToThread(thread);
    thread->start();
  }
  else 
    init();
}

Component::~Component() {
	if(thread) {
		// last chance to get rid of the thread. 
		// components should handle that in their own destructors
		thread->terminate();
		delete thread;
	}
}

void ComponentThreader::run() {
  try {
    owner->runInit();
    exec();
  } catch (char const * error) {
	    cerr << error << endl;
	    throw error;
  }
}
